# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=numpy

pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=1.8.1
pkgrel=1
pkgdesc="Scientific tools for Python (mingw-w64)"
arch=('any')
license=('BSD')
url="http://www.numpy.org/"
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-lapack"
  "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
  "${MINGW_PACKAGE_PREFIX}-python3-nose"
  "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
  "${MINGW_PACKAGE_PREFIX}-python2-nose"
  "${MINGW_PACKAGE_PREFIX}-gcc-fortran"
)
options=('staticlibs')
source=(http://downloads.sourceforge.net/numpy/${_realname}-${pkgver}.tar.gz
    fix-finding-python2.patch)
md5sums=('be95babe263bfa3428363d6db5b64678'
         '3b28e649316bcbc7fcc88a8c8498b817')

prepare() {
  cd ${_realname}-$pkgver
  patch -Np1 -i ${srcdir}/fix-finding-python2.patch
  cd ..

  cp -a numpy-$pkgver numpy-py2-$pkgver
  cp -a numpy-$pkgver numpy-py3-$pkgver
  cd numpy-py2-$pkgver

  sed -e "s|#![ ]*/usr/bin/python$|#!/usr/bin/python2|" \
    -e "s|#![ ]*/usr/bin/env python$|#!/usr/bin/env python2|" \
    -e "s|#![ ]*/bin/env python$|#!/usr/bin/env python2|" \
    -i $(find . -name '*.py')
}

build() {
  export ATLAS=None
  export LDFLAGS="$LDFLAGS -shared"

  echo "Building Python2"
  cd numpy-py2-$pkgver
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py config_fc --fcompiler=gnu95 build

  echo "Building Python3"
  cd ../numpy-py3-$pkgver
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py config_fc --fcompiler=gnu95 build
}

package_python2-numpy() {
  depends=("${MINGW_PACKAGE_PREFIX}-lapack" "${MINGW_PACKAGE_PREFIX}-python2")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python2-nose: testsuite")

  _pyver=2.7

  export ATLAS=None
  export LDFLAGS="$LDFLAGS -shared"

  cd numpy-py2-$pkgver
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py config_fc --fcompiler=gnu95 install --prefix=${MINGW_PREFIX} --root="${pkgdir}" --optimize=1

  install -m755 -d "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-numpy"
  install -m644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-numpy/"

  install -m755 -d "${pkgdir}${MINGW_PREFIX}/include/python${_pyver}"
  cp -rf ${pkgdir}${MINGW_PREFIX}/lib/python${_pyver}/site-packages/numpy/core/include/numpy "${pkgdir}${MINGW_PREFIX}/include/python${_pyver}/"

  # fix python command in files
  pushd ${MINGW_PREFIX} > /dev/null
  local _mingw_prefix=`pwd -W`
  popd > /dev/null
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_f}
  done
}

package_python3-numpy() {
  depends=("${MINGW_PACKAGE_PREFIX}-lapack" "${MINGW_PACKAGE_PREFIX}-python3")
  optdepends=("${MINGW_PACKAGE_PREFIX}-python-nose: testsuite")

  _pyver=3.4
  _pyinc=3.4m

  export ATLAS=None
  export LDFLAGS="$LDFLAGS -shared"

  cd numpy-py3-$pkgver
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py config_fc --fcompiler=gnu95 install --prefix=${MINGW_PREFIX} --root="${pkgdir}" --optimize=1

  install -m755 -d "${pkgdir}${MINGW_PREFIX}/share/licenses/python-numpy"
  install -m644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python-numpy/"

  install -m755 -d "${pkgdir}${MINGW_PREFIX}/include/python${_pyinc}"
  cp -rf ${pkgdir}${MINGW_PREFIX}/lib/python${_pyver}/site-packages/numpy/core/include/numpy "${pkgdir}${MINGW_PREFIX}/include/python${_pyinc}/"

  # fix python command in files
  pushd ${MINGW_PREFIX} > /dev/null
  local _mingw_prefix=`pwd -W`
  popd > /dev/null
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_f}
  done
}

package_mingw-w64-i686-python2-numpy() {
  install=${_realname}2-${CARCH}.install
  package_python2-numpy
}

package_mingw-w64-i686-python3-numpy() {
  install=${_realname}3-${CARCH}.install
  package_python3-numpy
}

package_mingw-w64-x86_64-python2-numpy() {
  install=${_realname}2-${CARCH}.install
  package_python2-numpy
}

package_mingw-w64-x86_64-python3-numpy() {
  install=${_realname}3-${CARCH}.install
  package_python3-numpy
}
